﻿' LibWowAPI
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Globalization
Imports System.Linq
Imports Newtonsoft.Json
Imports roncliProductions.LibWowAPI.Enums
Imports roncliProductions.LibWowAPI.Extensions

Namespace roncliProductions.LibWowAPI.Item

    ''' <summary>
    ''' A class that retrieves item information from the Blizzard WoW API.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ItemLookup
        Inherits WowAPIData

        Private ilItem As New Schema.item

#Region "WowAPIData Overrides"

#Region "Public Properties"

        ''' <summary>
        ''' The options for looking up an item.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Options As New ItemLookupOptions

        ''' <summary>
        ''' The length of time the data should be cached for, defaulting to 30 days for item information.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Property CacheLength As New TimeSpan(30, 0, 0, 0)

#End Region

#Region "Protected Properties"

        Protected Overrides ReadOnly Property CacheKey As String
            Get
                Return String.Format(CultureInfo.InvariantCulture, "LibWowAPI.Item.{0}", Options.ItemID)
            End Get
        End Property

        Protected Overrides ReadOnly Property URI As Uri
            Get
                Return New Uri(String.Format(CultureInfo.InvariantCulture, "/api/wow/item/{0}", Options.ItemID), UriKind.Relative)
            End Get
        End Property

#End Region

#End Region

#Region "Properties"

        Private iItem As Item
        ''' <summary>
        ''' The item returned from the Blizzard WoW API.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Item As Item
            Get
                Return iItem
            End Get
        End Property

#End Region

#Region "Methods"

#Region "Constructors"

        ''' <summary>
        ''' A default constructor to retrieve item information from the Blizzard WoW API.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' A constructor to retrieve item information for an item from the Blizzard WoW API.
        ''' </summary>
        ''' <param name="intItemID"></param>
        ''' <remarks></remarks>
        Public Sub New(intItemID As Integer)
            Options.ItemID = intItemID
            Load()
        End Sub

#End Region

        ''' <summary>
        ''' Loads the item.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()
            ilItem = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.item)), Schema.item)
            iItem = New Item(
                ilItem.id,
                ilItem.description,
                ilItem.name,
                ilItem.icon,
                ilItem.stackable,
                CType(ilItem.itemBind, Binding),
                If(ilItem.bonusStats.Count = 0, Nothing,
                    (
                        From s In ilItem.bonusStats
                        Select New BonusStats(
                            CType(s.stat, Stat),
                            s.amount,
                            s.reforged
                            )
                        ).ToCollection()
                    ),
                If(ilItem.itemSpells.Count = 0, Nothing,
                    (
                        From s In ilItem.itemSpells
                        Select New ItemSpell(
                            s.spellId,
                            New Spell(
                                s.spell.id,
                                s.spell.name,
                                s.spell.subtext,
                                s.spell.icon,
                                s.spell.description,
                                s.spell.range,
                                s.spell.castTime
                                ),
                            s.nCharges,
                            s.consumable,
                            s.categoryId
                            )
                        ).ToCollection()
                    ),
                ilItem.buyPrice,
                ilItem.itemClass.GetItemClass(),
                ilItem.itemSubClass,
                ilItem.containerSlots,
                If(ilItem.weaponInfo Is Nothing, Nothing,
                    New WeaponInfo(
                        (
                            From d In ilItem.weaponInfo.damage
                            Select New Damage(
                                d.minDamage,
                                d.maxDamage
                                )
                            ).ToCollection(),
                        ilItem.weaponInfo.weaponSpeed,
                        ilItem.weaponInfo.dps
                        )
                    ),
                CType(ilItem.inventoryType, InventoryType),
                ilItem.equippable,
                ilItem.itemLevel,
                ilItem.maxCount,
                ilItem.maxDurability,
                ilItem.minFactionId,
                CType(ilItem.minReputation, Standing),
                CType(ilItem.quality, Quality),
                ilItem.sellPrice,
                CType(ilItem.requiredSkill, Profession),
                ilItem.requiredLevel,
                ilItem.requiredSkillRank,
                If(ilItem.socketInfo Is Nothing OrElse ilItem.socketInfo.sockets.Count = 0, Nothing,
                    (
                        From s In ilItem.socketInfo.sockets
                        Select s.type
                        ).ToCollection()
                    ),
                If(ilItem.itemSource Is Nothing, Nothing,
                    New ItemSource(
                        ilItem.itemSource.sourceId,
                        ilItem.itemSource.sourceType
                        )
                    ),
                ilItem.baseArmor,
                ilItem.hasSockets,
                ilItem.isAuctionable
                )
        End Sub

#End Region

    End Class

End Namespace
